package com.cat.model.biz.service.api.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alonew.db.dao.TxCommonDAO;
import com.alonew.json.JSONArray;
import com.alonew.json.JSONException;
import com.alonew.json.JSONObject;
import com.cat.model.biz.service.api.AccountService;
import com.cat.model.biz.service.bs.UserBS;
import com.cat.model.common.model.common.AccountDVO;
import com.cat.model.common.model.common.AccountVO;
import com.cat.model.common.model.common.AcntWithdrawVO;
import com.cat.model.common.model.common.UserInfoVO;
import com.cat.model.common.util.ErrorCode;
import com.cat.model.common.util.PageUtil;
import com.cat.model.dao.mapper.ModelMapper;
import com.cat.model.dao.mapper.UserInfoMapper;
import com.cat.model.web.bean.AccountBean;

@Service("accountService")
public class AccountServiceImpl implements AccountService{
	
	@Autowired
	UserBS userBS;
	
	@Autowired
	UserInfoMapper userMapper;
	
	@Autowired
	ModelMapper modelMapper;

	@Autowired
	private TxCommonDAO txCommonDAO;
	
	@Override
	public String accountInfo(long userId) {
		UserInfoVO userInfo = userMapper.getUserInfo(userId);
		if(null == userInfo){
			return ErrorCode.OK.response();
		}
		
		AccountVO account = modelMapper.getAccount(userId);
		
		JSONObject json = ErrorCode.OK.responseJSON();
		try {
			JSONObject infoJson = new JSONObject();
			infoJson.put("accountId", account.getId());
			infoJson.put("balance", account.getBalance());
			infoJson.put("availBalance", account.getAvailBalance());
			
			json.put("data", infoJson);
		} catch (JSONException e) {
			return ErrorCode.SERVER_ERROR.response();
		}
		
		return json.toString();
	}
	
	@Override
	public String getAccountDList(Long userId,Long accountId, int page, int size) {
		List<AccountDVO> accDList = modelMapper.getAccountDList(accountId);
		
		JSONObject json = ErrorCode.OK.responseJSON();
		List<AccountBean> beanList = new ArrayList<AccountBean>();
		if(accDList == null || accDList.size() <= 0){
			try {
				json.put("data", new JSONArray(beanList));
			} catch (JSONException e) {
				return ErrorCode.SERVER_ERROR.response();
			}
			return json.toString();
		}
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		for(AccountDVO vo : accDList){
			AccountBean bean = new AccountBean();
			
			bean.setAmount(vo.getAmount());
			bean.setCreateTime(sdf.format(vo.getCreateTime()));
			bean.setRefBizCode(vo.getRefBizCode());
			bean.setRefBizId(vo.getRefBizId());
			bean.setRefBizName(vo.getRefBizName());
			bean.setBalance(vo.getBalance());
			
			beanList.add(bean);
		}
		
		 int[] indexs = PageUtil.getStartAndEnd(page, size, beanList.size());
		 List<AccountBean> resultList = beanList.subList(indexs[0], indexs[1]);
		
		try {
			json.put("data", new JSONArray(resultList));
		} catch (JSONException e) {
			return ErrorCode.SERVER_ERROR.response();
		}
		
		return json.toString();
	}

	@Override
	public String getWithdrawList(Long userId,  int page,int size) {
		List<AcntWithdrawVO> acntList = modelMapper.getAcntWithdrawByUserId(userId.longValue());
		
		JSONObject json = ErrorCode.OK.responseJSON();
		List<AccountBean> beanList = new ArrayList<AccountBean>();
		if(acntList == null || acntList.size() <= 0){
			try {
				json.put("data", new JSONArray(beanList));
			} catch (JSONException e) {
				return ErrorCode.SERVER_ERROR.response();
			}
			return json.toString();
		}
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		for(AcntWithdrawVO vo : acntList){
			AccountBean bean = new AccountBean();
			
			bean.setAmount(vo.getAmount());
			bean.setCreateTime(sdf.format(vo.getCreateTime()));
			String approveStatusDesc = "";
			if("01".equals(vo.getApproveStatus())){
				approveStatusDesc = "待复核";
			} else if("02".equals(vo.getApproveStatus())){
				approveStatusDesc = "驳回";
			} else if("03".equals(vo.getApproveStatus())){
				approveStatusDesc = "审核通过";
			} else if("04".equals(vo.getApproveStatus())){
				approveStatusDesc = "已转账";
			}
			bean.setApproveStatus(approveStatusDesc);
			
			beanList.add(bean);
		}
		
		 int[] indexs = PageUtil.getStartAndEnd(page, size, beanList.size());
		 List<AccountBean> resultList = beanList.subList(indexs[0], indexs[1]);
		
		try {
			json.put("data", new JSONArray(resultList));
		} catch (JSONException e) {
			return ErrorCode.SERVER_ERROR.response();
		}
		
		return json.toString();
	}

	@Override
	public String withdrawApply(Long userId, AcntWithdrawVO withdraw) {
		withdraw.setUserId(userId);
		withdraw.setCreateTime(new Date());
		withdraw.setApproveStatus("01");//01-待复核
		
		txCommonDAO.insert(withdraw);
		
		return ErrorCode.OK.response();
	}

}
